
#include "scatter_elements_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"   //GetPlatformInfo类接口
#include <algorithm>

namespace optiling {

const uint32_t BLOCK_SIZE = 32;   //32bytes数据对齐
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    ScatterElementsTilingData tiling;

    //x, indices, updates
    auto shape_x =        context->GetInputShape(0)->GetOriginShape();
    auto shape_indices =  context->GetInputShape(1)->GetOriginShape();
    auto shape_updates =  context->GetInputShape(2)->GetOriginShape();

    int32_t x_size = 1;          //总的元素个数
    int32_t indices_size = 1;
    int32_t updates_size = 1;

    int32_t x_dimensional;       //维度数
    int32_t indices_dimensional;
    int32_t updates_dimensional;

    int32_t x_ndarray[20];       //用来存储每一维的数据大小
    int32_t indices_ndarray[20];
    int32_t updates_ndarray[20];
    uint8_t reduce;
    
    //求 x 的元素、维度和数据
    x_dimensional = shape_x.GetDimNum();
    //printf("x_dimensional is %d\n",x_dimensional);
    for(int i = 0; i < x_dimensional; i++)
    {
        x_ndarray[i] = shape_x.GetDim(i);
        //printf("x_ndarray %d is %d\n",i,x_ndarray[i] );
        x_size *= x_ndarray[i];
        //printf("x_size is %d\n",x_size );
    }

    //求 indices 的元素、维度和数据
    indices_dimensional = shape_indices.GetDimNum();
    //printf("indices_dimensional is %d\n",indices_dimensional);
    for(int i = 0; i < indices_dimensional; i++)
    {
        indices_ndarray[i] = shape_indices.GetDim(i);
        indices_size *= indices_ndarray[i];
    }

    //求 updates 的元素、维度和数据
    updates_dimensional = shape_updates.GetDimNum();
    //printf("updates_dimensional is %d\n",updates_dimensional);
    for(int i = 0; i < updates_dimensional; i++)
    {
        updates_ndarray[i] = shape_updates.GetDim(i);
        updates_size *= updates_ndarray[i];
    }

    //传递轴和reduce参数
    int axis = *context->GetAttrs()->GetInt(0);
    const char attr0 = *context->GetAttrs()->GetStr(1);
    
    reduce = static_cast<uint8_t>(attr0);
    //memcpy(reduce, attr0, 1);
    // printf("axis is %d",axis);
    // printf("reduce is %c",reduce); //[0]

    tiling.set_x_size(x_size);
    tiling.set_indices_size(indices_size);
    tiling.set_updates_size(updates_size);
    tiling.set_x_ndarray(x_ndarray);
    tiling.set_indices_ndarray(indices_ndarray);
    tiling.set_updates_ndarray(updates_ndarray);
    tiling.set_x_dimensional(x_dimensional);
    tiling.set_indices_dimensional(indices_dimensional);
    tiling.set_updates_dimensional(updates_dimensional);
    tiling.set_axis(axis);
    tiling.set_reduce(reduce);


    context->SetBlockDim(1);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = 0;
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class ScatterElements : public OpDef {
public:
    explicit ScatterElements(const char* name) : OpDef(name)
    {
        this->Input("var")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_UINT8, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("indices")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("updates")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_UINT8, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("var")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_UINT8, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        
        this->Attr("axis").AttrType(OPTIONAL).Int(0);
        this->Attr("reduce").AttrType(OPTIONAL).String("");
        
        
        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc)
            .AddConfig("ascend310b");
    }
};

OP_ADD(ScatterElements);
}
